﻿Public Class ViewFormat

    Private Sub Button_Click_1(sender As Object, e As Windows.RoutedEventArgs)
        DataContext.DecimalCount = Math.Max(0, DataContext.DecimalCount - 1)
    End Sub

    Private Sub Button_Click_2(sender As Object, e As Windows.RoutedEventArgs)
        DataContext.DecimalCount = Math.Min(6, DataContext.DecimalCount + 1)

    End Sub
End Class


Public Class ViewModelFormat
    Inherits UIObject

    Private Shared _Cultures As List(Of String)
    Public ReadOnly Property Cultures As List(Of String)
        Get
            If _Cultures Is Nothing Then
                _Cultures = (From c In Globalization.CultureInfo.GetCultures(Globalization.CultureTypes.AllCultures)
                            Order By c.Name
                            Select c.Name).ToList()

                _Cultures.Insert(0, "(neutre)")
                _Cultures.Insert(0, "(actuelle)")
            End If
            Return _Cultures
        End Get
    End Property

    Private Shared _TypeFormats As List(Of String)
    Public ReadOnly Property TypeFormats As List(Of String)
        Get
            If _TypeFormats Is Nothing Then
                _TypeFormats = New List(Of String)()
                _TypeFormats.Add("Nombre")
                _TypeFormats.Add("Date et Heure")
            End If
            Return _TypeFormats
        End Get
    End Property


    Private Shared _NumberFormats As List(Of String)
    Private Shared _NumberFormatCodes() As String = {"C", "N", "P"} ' {"C", "D", "E", "e", "F", "G", "N", "P", "R", "X", "x"}
    Private Shared _UsedNumber As Double = Math.PI * 100000.0

    Private Shared _DateFormats As List(Of String)
    Private Shared _UsedDate As DateTime = Now
    Private Shared _DateFormatCodes() As String = {"g", "d", "D", "f", "F", "g", "G", "M", "O", "R", "s", "t", "T", "u", "U", "Y"}

    Public ReadOnly Property GroupFormats As List(Of String)
        Get
            If _NumberFormats Is Nothing Then
                _NumberFormats = (From s In _NumberFormatCodes Select _UsedNumber.ToString(s & DecimalCount, GetCultureInfo(Me.Culture))).ToList()
                '_NumberFormats.Add(_UsedNumber.ToString("#,##0,,K", GetCultureInfo(Me.Culture)))
                '_NumberFormats.Add(_UsedNumber.ToString("##0,,,M" & DecimalCount, GetCultureInfo(Me.Culture)))
            End If

            If _DateFormats Is Nothing Then
                _DateFormats = (From s In _DateFormatCodes Select _UsedDate.ToString(s, GetCultureInfo(Me.Culture))).ToList()
            End If

            If TypeFormat = "Nombre" Then Return _NumberFormats
            If TypeFormat = "Date et Heure" Then Return _DateFormats

            Return Nothing
        End Get
    End Property


    Private _Culture As String = "(actuelle)"
    Public Property Culture As String
        Get
            Return _Culture
        End Get
        Set(ByVal value As String)
            _Culture = value
            OnPropertyChanged("Culture")
            OnPropertyChanged("PreviewText")
        End Set
    End Property

    Private _TypeFormat As String = "Nombre"
    Public Property TypeFormat As String
        Get
            Return _TypeFormat
        End Get
        Set(ByVal value As String)
            _TypeFormat = value
            OnPropertyChanged("TypeFormat")
            OnPropertyChanged("GroupFormats")
            OnPropertyChanged("GroupFormat")
            OnPropertyChanged("PreviewText")
        End Set
    End Property

    Private _DecimalCount As Integer = 2
    Public Property DecimalCount As Integer
        Get
            Return _DecimalCount
        End Get
        Set(ByVal value As Integer)
            _DecimalCount = value
            OnPropertyChanged("DecimalCount")
            OnPropertyChanged("PreviewText")
        End Set
    End Property

    Private _GroupFormat As Integer
    Public Property GroupFormat As Integer
        Get
            Return _GroupFormat
        End Get
        Set(ByVal value As Integer)
            _GroupFormat = value
            OnPropertyChanged("GroupFormat")
            OnPropertyChanged("PreviewText")
        End Set
    End Property

    Public ReadOnly Property PreviewText As String
        Get
            Try
                If TypeFormat = "Nombre" Then Return _UsedNumber.ToString(_NumberFormatCodes(_GroupFormat) & DecimalCount, GetCultureInfo(Me.Culture))
                If TypeFormat = "Date et Heure" Then Return _UsedDate.ToString(_DateFormatCodes(_GroupFormat), GetCultureInfo(Me.Culture))
            Catch ex As Exception
                Return ""
            End Try

            Return ""
        End Get
    End Property

    Friend Function GetFormat() As String
        Try
            If TypeFormat = "Nombre" Then Return _NumberFormatCodes(_GroupFormat) & DecimalCount
            If TypeFormat = "Date et Heure" Then Return _DateFormatCodes(_GroupFormat)
        Catch ex As Exception
            Return ""
        End Try

        Return ""
    End Function

    Private Shared Function GetCultureInfo(culturename) As Globalization.CultureInfo
        If culturename = "(actuelle)" Then Return Globalization.CultureInfo.CurrentCulture
        If culturename = "(neutre)" Then Return Globalization.CultureInfo.InvariantCulture
        Return Globalization.CultureInfo.GetCultureInfo(culturename)
    End Function
End Class